package com.novabluetooth.data;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.Toast;

public class GlobalContsBlueThooth {
	public static final int RequestXueya = 0;
	public static final int RequestTizhong = 1;
	public static final int RequestXuetang = 2;
	public static final int RequestXueyang = 3;
	public static final int RequestTiwen = 4;
	public static final int RequestXueHongDanbai = 5;
	public static final int RequestNiaoChangGui = 6;
	public static final int RequestMaiLv = 7;
	public static final int RequestLiBangECG = 8;
	public static final int RequestXinLv = 9;

	/**
	 * 设置不同的数据显示
	 * 
	 * @author 王婷婷
	 * @date 2017年7月14日 上午9:58:18
	 */
	public static SpannableStringBuilder setDifferentText(String str_content, int start, int end) {

		if(TextUtils.isEmpty(str_content)){
			return null;
		}
		if(end>str_content.length()){
			return null;
		}
		SpannableStringBuilder builder = new SpannableStringBuilder(str_content);
		ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.RED);
		builder.setSpan(yellowSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

		return builder;
	}
	/**
	 * 判断相差时间
	 * 
	 * @param stime
	 * @param etime
	 * @return
	 * @author 王婷婷
	 * @date 2015年12月12日 上午11:02:16
	 */
	public static int getAge(Date bornTime) {
		int age = -1;
		try {
			// bornDate = (Date) dateFormat.parse(bornTime);
			java.util.Calendar born = java.util.Calendar.getInstance();
			java.util.Calendar now = java.util.Calendar.getInstance();
			if (bornTime != null) {
				// born.setTime(bornDate);
				born.setTime(bornTime);
				now.setTime(new Date());
				age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
				if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
					age -= 1;
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return age;

	}
	/**
	 * 获取最新修改的文件名
	 * 
	 * @param path
	 * @return
	 * @author 王婷婷
	 * @date 2017年3月20日 上午11:59:31
	 */

	public static String getLastModifiedName(String path) {

		String fileName = "";
		// 判断是否有外存储设备
		long lastModified = 0l;// 最后一次修改时间
		if (Environment.getExternalStorageState().equals(

				Environment.MEDIA_MOUNTED)) {
			Log.w("XML-----eeeeeeeeeeeeeeeeeeee", path);
			File filePath = new File(path);
			if (!filePath.exists()) {
				Log.w("XML-----eeeeeeeeeeeeeeeeeeee", "文件不存在---");
				return fileName;
			}
			File lastFile = null;// 最新修改的文件

			File[] files = filePath.listFiles();
			if (files.length > 1) {
				// 默认有个.txt文件

				for (File file : files) {
					Map<String, Object> map = new HashMap<String, Object>();

					fileName = file.getName();

					// 获取.xml后缀的文件

					if (fileName.endsWith(".xml")) {
						// 当前文件的修改时间比上次的更大就将最新修改时间记录为当前文件的修改时间。以便下一次比较

						if (file.lastModified() > lastModified) {
							lastModified = file.lastModified();
							lastFile = file;
						}

						Log.w("XML-----eeeeeeeeeeeeeeeeeeee", fileName);
					}

				}
				fileName = lastFile.getName();
			} else {
				// 没有文件
				fileName = "";
			}

			// getFileName(files);

		}
		return fileName;

	}

	public static void showToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
}
